import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:nft_shop/main.dart';
import 'package:nft_shop/my/conteroller/my_conteroller.dart';
import 'package:nft_shop/my/page/my_list_type_page.dart';
import 'package:nft_shop/res/global.dart';

class MyListPage extends StatefulWidget {
  int? index;

  MyListPage({Key? key, this.index}) : super(key: key);

  @override
  _MyListPageState createState() => _MyListPageState();
}

class _MyListPageState extends State<MyListPage> with TickerProviderStateMixin,AutomaticKeepAliveClientMixin {
  ///page
  final PageController _pageController = PageController(initialPage: 0);
  /// 当前第几页
  int currentPageNum = 0;

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    eventBus.on<String>().listen((it) {
      if(it == "logout"){
        _jumpToPage(0);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        tabsWidget(),
        Expanded(
          child: PageView.builder(
            physics: new NeverScrollableScrollPhysics(),
            itemCount: 3,
            onPageChanged: (index){
              setState(() {
                currentPageNum = index;
              });
            },
            controller: _pageController,
            itemBuilder: (_, index) {
              return MyListTypePage(section: widget.index,row:index,);
            },
          ),
        ),
      ],
    );
  }

  ///选项卡
  Widget tabsWidget() {
    return Container(
        height: 30,
        width:  55*3,
        margin: EdgeInsets.only(top: 10,left: 15),
        decoration: BoxDecoration(
          color: Global.kTheme.colorBackground4,
          borderRadius: BorderRadius.circular(4),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            InkWell(
              onTap: (){
                if (!mounted) {
                  return;
                }
                _jumpToPage(0);
              },
              child: Container(
                width: 55,
                decoration: BoxDecoration(
                  color: currentPageNum == 0?Global.kTheme.colorBackground5:Colors.transparent,
                  borderRadius: BorderRadius.circular(4),
                ),
                child: Center(
                    child: Text(
                  "全部",
                  style: TextStyle(
                      fontFamily: Global.mySemiboldFont,
                      fontSize: 12,
                      color: currentPageNum == 0?Global.kTheme.textPrimary1:Global.kTheme.textPrimary3),
                )),
              ),
            ),
            InkWell(
              onTap: (){
                if (!mounted) {
                  return;
                }
                _jumpToPage(1);
              },
              child: Container(
                width: 55,
                decoration: BoxDecoration(
                  color: currentPageNum == 1?Global.kTheme.colorBackground5:Colors.transparent,
                  borderRadius: BorderRadius.circular(4),
                ),
                child: Center(
                    child: Text(
                  "藏品",
                  style: TextStyle(
                      fontFamily: Global.mySemiboldFont,
                      fontSize: 12,
                      color: currentPageNum == 1?Global.kTheme.textPrimary1:Global.kTheme.textPrimary3),
                )),
              ),
            ),
            InkWell(
              onTap: (){
                if (!mounted) {
                  return;
                }
                _jumpToPage(2);
              },
              child: Container(
                width: 55,
                decoration: BoxDecoration(
                  color: currentPageNum == 2?Global.kTheme.colorBackground5:Colors.transparent,
                  borderRadius: BorderRadius.circular(4),
                ),
                child: Center(
                    child: Text(
                  "盲盒",
                  style: TextStyle(
                      fontFamily: Global.mySemiboldFont,
                      fontSize: 12,
                      color: currentPageNum == 2?Global.kTheme.textPrimary1:Global.kTheme.textPrimary3),
                )),
              ),
            ),
          ],
        ));
  }

  void _jumpToPage(int index){
    _pageController.jumpToPage(index);
    MyConteroller.to.rowIndex = index;
    MyConteroller.to.refreshData();
  }

}
